package com.freemarker.product;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateNumberModel;
import static freemarker.template.ObjectWrapper.DEFAULT_WRAPPER;

public class ArticleDirective implements TemplateDirectiveModel{

	@Override
	public void execute(Environment env, Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body) throws TemplateException, IOException {
		Object paramValue = params.get("articleId");
		int id = 0;
		if(paramValue instanceof TemplateNumberModel){
			id = ((TemplateNumberModel)paramValue).getAsNumber().intValue();
		}
		System.out.println(id);
		List<String> list = new ArrayList<String>();
		list.add("a");
		list.add("b");
		list.add("c");
		list.add("d");
		env.setVariable("title", DEFAULT_WRAPPER.wrap("标题"));
		env.setVariable("article", DEFAULT_WRAPPER.wrap(list));
		body.render(env.getOut());
	}

}
